package com.ttctracker.map;

import android.content.Context;
import android.os.Handler;
import com.skyhookwireless.wps.IPLocation;
import com.skyhookwireless.wps.IPLocationCallback;
import com.skyhookwireless.wps.WPS;
import com.skyhookwireless.wps.WPSAuthentication;
import com.skyhookwireless.wps.WPSContinuation;
import com.skyhookwireless.wps.WPSLocation;
import com.skyhookwireless.wps.WPSLocationCallback;
import com.skyhookwireless.wps.WPSPeriodicLocationCallback;
import com.skyhookwireless.wps.WPSReturnCode;
import com.skyhookwireless.wps.WPSStreetAddressLookup;
import com.skyhookwireless.wps.XPS;
import com.ttctracker.util.MyApp;

public class SkyHookHelper {
	private final MyWPSLocationCallback locationCallback=new MyWPSLocationCallback();
	private Context context;
	private WPS wps;
	private XPS xps;
	private static WPSAuthentication auth;
	public static final int LOCATION_MESSAGE = 1;
	public static final int ERROR_MESSAGE = 2;
	public static final int DONE_MESSAGE = 3;
	public static final int FIRST_TIME_MESSAGE=4;
	private Handler locationHandler;
	private static final int PERIOD=2*60*1000; //two minutes
	private static final int DESIRED_XPS_ACCURACY=30;
	private boolean stop=false;
	private boolean isFirstTime=true;
	
	public SkyHookHelper(Context context,Handler locationHandler){
		//wps=new WPS(context);
		xps=new XPS(context);
		this.locationHandler=locationHandler;
		if (auth==null) {
			auth=new WPSAuthentication(MyApp.SKYHOOK_USERNAME, MyApp.SKYHOOK_REALM);
		}
	}
	
	public void getUserLocation() {
		if (wps!=null) {
			wps.getLocation(auth, WPSStreetAddressLookup.WPS_NO_STREET_ADDRESS_LOOKUP, locationCallback);
			
		}
		if (xps!=null) {
			xps.getXPSLocation(auth, (int)PERIOD/1000, DESIRED_XPS_ACCURACY, locationCallback);
			//xps.getLocation(auth, WPSStreetAddressLookup.WPS_NO_STREET_ADDRESS_LOOKUP, locationCallback);
		}
		
	}
	
	public void toggleGPS(boolean stop){
		this.stop=stop;
		if (!this.stop) {
			isFirstTime=true;
		}
	}
	
	
	
	
	private class MyWPSLocationCallback implements WPSLocationCallback,IPLocationCallback, WPSPeriodicLocationCallback {

		// private static final MyWPSLocationCallback INSTANCE=new
		// MyWPSLocationCallback();
		//private WPSLocation currentLocation;
		//private IPLocation currentIpLocation;
		public MyWPSLocationCallback() {
			//currentLocation = null;
		//	currentIpLocation=null;
		}

		

		@Override
		public void done() {
			locationHandler.sendMessage(locationHandler.obtainMessage(DONE_MESSAGE));
		}

		@Override
		public WPSContinuation handleError(WPSReturnCode arg0) {
			//currentLocation=null;
			locationHandler.sendMessage(locationHandler.obtainMessage(ERROR_MESSAGE, arg0));
			return WPSContinuation.WPS_STOP;
		}

		@Override
		public void handleWPSLocation(WPSLocation location) {
			//currentLocation = location;
			
			locationHandler.sendMessage(locationHandler.obtainMessage(FIRST_TIME_MESSAGE, location));
		}

		@Override
		public void handleIPLocation(IPLocation location) {
			locationHandler.sendMessage(locationHandler.obtainMessage(FIRST_TIME_MESSAGE, location));
		}

		@Override
		public WPSContinuation handleWPSPeriodicLocation(WPSLocation location) {
			if (isFirstTime) {
				isFirstTime=false;
				//locationHandler.searchNearbyRoutes(location);
				
				locationHandler.sendMessage(locationHandler.obtainMessage(FIRST_TIME_MESSAGE, location));
			}else {
				locationHandler.sendMessage(locationHandler.obtainMessage(LOCATION_MESSAGE, location));
			}
		
			/*if (! stop)
                return WPSContinuation.WPS_CONTINUE;
            else*/
                return WPSContinuation.WPS_STOP;
		}

	}
}
